package util;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;

public class Image {
	
	public static void scaleWidth(String src, int width, String dest) throws IOException {
		BufferedImage bsrc = ImageIO.read(new File(src));
		
		int w = bsrc.getWidth();
		int h = bsrc.getHeight();
		
		int height = Math.round((h * width) / w);
		
		createImage(bsrc, width, height, dest);
	}

    public static void scaleHeight(String src, int height, String dest) throws IOException {
		BufferedImage bsrc = ImageIO.read(new File(src));

		int w = bsrc.getWidth();
		int h = bsrc.getHeight();

		int width = Math.round((w * height) / h);

		createImage(bsrc, width, height, dest);
	}

    private static void createImage(BufferedImage src, int width, int height, String dest) {
        BufferedImage bdest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

		Graphics2D g = bdest.createGraphics();
		AffineTransform at = AffineTransform.getScaleInstance((double)width/src.getWidth(), (double)height/src.getHeight());
		g.drawRenderedImage(src,at);
        try {
            ImageIO.write(bdest, "JPG", new File(dest));
        } catch (IOException ex) {
            Logger.getLogger(Image.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}